package com.itheima.health.service.impl;


import com.itheima.health.entity.MemberDto;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrdersettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.itheima.health.dao.OrderDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrdersettingService ordersettingService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 任务10 体检预约
     *
     * @param memberDto
     */
    @Transactional
    @Override
    public Order submit(MemberDto memberDto, HttpSession httpSession) {
        //判断当天预约人数是否已满
        Date orderDate = memberDto.getOrderDate();
        OrderSetting orderSetting = ordersettingService.findByDate(orderDate);
        if (orderSetting.getReservations() + 1 > orderSetting.getNumber()) {
            throw new BusinessRuntimeException("当天预约人数已满,请重新选择");
        } else {
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            log.info("[更改预约人数+1],当前预约人数:{}", orderSetting.getReservations());
            ordersettingService.updateReservations(orderSetting);
        }
        //判断验证码
        String redisKey = "ORDER" + ":" + memberDto.getTelephone();
        String validateCodeRedis = (String) redisTemplate.opsForValue().get(redisKey);
        log.info("[获取验证码],{}", validateCodeRedis);
        if (!memberDto.getValidateCode().equals(validateCodeRedis)) {
            throw new BusinessRuntimeException("验证码错误,请重新输入");
        }
        //判断当前用户是否已注册为会员
        memberDto.setPhoneNumber(memberDto.getTelephone());
        Member member = memberService.findByCondition(memberDto);
        if (member == null) {
            //注册为会员
            memberDto.setRegTime(new Date());
            log.info("[注册会员],memberDto:{}", memberDto);
            memberService.insert(memberDto);
        } else {
            //设置返回id数据
            memberDto.setId(memberService.getIdByCondition(memberDto));
        }
        //复制和填写order信息
        Order order = new Order();
        BeanUtils.copyProperties(memberDto, order, "id");
        log.info("[order直接复制],order:{}", order);
        order.setMemberId(memberDto.getId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(memberDto.getSetmeal_id());
        //保存预约信息
        orderDao.insert(order);
        log.info("[预约保存],order:{}", order);
        return order;
    }

    @Autowired
    private OrderDao orderDao;

    /**
     * 任务11 体检预约-订单数据展示
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById(Integer id) {
        // 将获取的数据存储在map集合
        Map orderInfo = orderDao.findById(id);

        //  将日期data--转化为String-->
        Object orderDate = orderInfo.get("orderDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = sdf.format(orderDate);
        // 将转换为String的vlue塞进Map集合里面
        orderInfo.put("orderDate", strDate);
        log.info("[回显数据]" + orderInfo);
        return orderInfo;
    }

    @Override
    public List<Map<String, Object>> getCountSetmeal() {
        return orderDao.getCountSetmeal();
    }
}
